import moment from 'moment'
export default {
	// 处理数据
	// 基本信息，时序图，是否是水情，目前就两种
	dealDataEv(info, monitorData, isWater = true) {
		// outq:出库流量，inq:入库流量，q:流量，z:水位，drp：雨量
		// , inq: '入库流量', q: '流量', z: '水位', drp: '雨量'
		const typeObj = {
			outq: { name: '出库流量', color: '#FF6B00' },
			inq: { name: '入库流量', color: '#3FD1B8' },
			q: { name: '流量', color: '#FF6B00' },
			z: { name: '水位', color: '#0085FF' },
			drp: { name: '雨量', color: '#0085FF' },

			alarmZ: { name: '警戒水位', color: '#FFC700' },
			guarantyZ: { name: '保证水位', color: '#FF0000' },
			floodLevel: { name: '汛限水位', color: '#FFC700' },
			floodHighLevel: { name: '防洪高水位', color: '#FF0000' }
		}

		// 图块 和图表数据，最后返回这两个
		let table = []
		let option = {}

		// 先处理时间
		Object.keys(monitorData).forEach((key) => {
			monitorData[key].map((t) => {
				t.time = moment(t.time).format('YYYY-MM-DD HH:mm')
				return t
			})
		})

		const findMaxAndMin = (listPost) => {
			if (!listPost || listPost.length == 0) {
				return {
					max: { time: '-', value: '-' },
					min: { time: '-', value: '-' }
				}
			}
			const list = listPost.filter((num) => typeof num.value === 'number' && !isNaN(num.value))
			if (list.length == 0) {
				return {
					max: { time: '-', value: '-' },
					min: { time: '-', value: '-' }
				}
			}
			const max = list.reduce((prev, curr) => (prev.value > curr.value ? prev : curr))
			const min = list.reduce((prev, curr) => (prev.value < curr.value ? prev : curr))
			return { max, min }
		}

		// 寻找值
		const findInfoItemByKey = (key) => {
			try {
				const infoData = info
				let fieldList = infoData.template.fieldList
				const result = fieldList.find((el) => el.name == key)

				if (result) {
					result.defaultValue = info.data[key] || '-'
					return result
				} else {
					throw new Error()
				}
			} catch (error) {
				return {
					defaultValue: '-',
					name: null,
					show: null,
					type: null
				}
			}
		}

		if (isWater) {
			// 生成最高最低最大最小
			const qmaxmin = findMaxAndMin(monitorData.q)
			const zmaxmin = findMaxAndMin(monitorData.z)
			table = [
				{ name: '最高水位', value: zmaxmin.max.value || '-', unit: 'm', date: zmaxmin.max.time || '-' },
				{ name: '最低水位', value: zmaxmin.min.value || '-', unit: 'm', date: zmaxmin.min.time || '-' },
				{ name: '最大流量', value: qmaxmin.max.value || '-', unit: 'm³/s', date: qmaxmin.max.time || '-' },
				{ name: '最小流量', value: qmaxmin.min.value || '-', unit: 'm³/s', date: qmaxmin.min.time || '-' }
			]
		} else {
			// 生成最高最低最大最小
			const zmaxmin = findMaxAndMin(monitorData.z)
			const inqmaxmin = findMaxAndMin(monitorData.inq)
			const outqmaxmin = findMaxAndMin(monitorData.outq)
			table = [
				{ name: '最高水位', value: zmaxmin.max.value || '-', unit: 'm', date: zmaxmin.max.time || '-' },
				{ name: '最低水位', value: zmaxmin.min.value || '-', unit: 'm', date: zmaxmin.min.time || '-' },
				{ name: '最大入库流量', value: inqmaxmin.max.value || '-', unit: 'm³/s', date: inqmaxmin.max.time || '-' },
				{ name: '最小入库流量', value: inqmaxmin.min.value || '-', unit: 'm³/s', date: inqmaxmin.min.time || '-' },
				{
					name: '最大出库流量',
					value: outqmaxmin.max.value || '-',
					unit: 'm³/s',
					date: outqmaxmin.max.time || '-'
				},
				{ name: '最小出库流量', value: outqmaxmin.min.value || '-', unit: 'm³/s', date: outqmaxmin.min.time || '-' }
			]
		}

		// 处理图表

		let xdata = []
		const series = []
		const legendName = []

		Object.keys(monitorData).forEach((key) => {
			let obj = typeObj[key]
			legendName.push(obj.name)

			let sinOps = {}
			// 雨量是柱状图
			if (key == 'drp') {
				sinOps = {
					name: obj.name,
					data: monitorData[key].map((e) => e.value || '-'),
					symbol: 'none',
					itemStyle: {
						color: obj.color,
						lineStyle: {
							width: 2,
							color: obj.color
						}
					},
					connectNulls: true,
					type: 'bar',
					smooth: true
				}
			} else {
				sinOps = {
					name: obj.name,
					data: monitorData[key].map((e) => e.value || '-'),
					symbol: 'none',
					yAxisIndex: ['流量', '出库流量', '入库流量'].includes(obj.name) ? 1 : 0,
					itemStyle: {
						color: obj.color,
						lineStyle: {
							width: 2,
							color: obj.color
						}
					},
					connectNulls: true,
					type: 'line',
					smooth: true
				}
			}
			series.push(sinOps)
			// xdata长度
			xdata = monitorData[key].map((item) => item.time)
		})

		let otherProps = []
		if (isWater) {
			otherProps = ['alarmZ', 'guarantyZ']
		} else {
			otherProps = ['floodLevel', 'floodHighLevel']
		}
		// 加载其他额外字段
		otherProps.forEach((key) => {
			const porps = findInfoItemByKey(key)
			if (porps.name) {
				let obj = typeObj[key]
				legendName.push(obj.name)
				series.push({
					name: obj.name,
					data: new Array(xdata.length).fill(porps.defaultValue || '-'),
					symbol: 'none',
					itemStyle: {
						color: obj.color,
						lineStyle: {
							width: 2,
							color: obj.color
						}
					},
					type: 'line',
					smooth: true
				})
			}
		})
		// 查找水位最大值和最小值
		let waterMaxMin = { max: 0, min: 0 }
		series.find((item) => {
			if (item.name == '水位') {
				const filteredNumbers = item.data.filter((num) => typeof num === 'number' && !isNaN(num))
				let max = Math.max(...filteredNumbers)
				let min = Math.min(...filteredNumbers)
				waterMaxMin.max = parseInt(max * 1.1)
				waterMaxMin.min = parseInt(min * 0.9)
			}
		})
		// 查找流量最大值和最小值
		let liuliangMaxMin = { max: 0, min: 0 }
		let arrList = []
		series.find((item) => {
			if (['流量', '出库流量', '入库流量'].includes(item.name)) {
				const filteredNumbers = item.data.filter((num) => typeof num === 'number' && !isNaN(num))
				let max = Math.max(...filteredNumbers)
				let min = Math.min(...filteredNumbers)
				arrList.push(max, min)
				// if (liuliangMaxMin.max < max) {
				// 	liuliangMaxMin.max = max
				// }
				// if (liuliangMaxMin.min > min) {
				// 	liuliangMaxMin.min = min
				// }
				// liuliangMaxMin.max = parseInt(max * 1.1)
				// liuliangMaxMin.min = parseInt(min * 0.9)
			}
		})
		if (arrList.length > 0) {
			liuliangMaxMin.max = Math.max(...arrList)
			liuliangMaxMin.min = Math.min(...arrList)
		}

		option = {
			xAxis: {
				data: xdata,
				axisLabel: {
					show: true,
					showMaxLabel: true,
					textStyle: {
						color: '#A0A0A0'
					},
					formatter: function (value, index) {
						return moment(value).format('HH:mm')
					}
				},
				splitLine: {
					show: true,
					lineStyle: {
						color: '#A0A0A0',
						width: 1
					}
				},
				axisLine: {
					show: true,
					lineStyle: {
						color: '#A0A0A0',
						width: 1
					}
				},
				// name: "日期",
				axisTick: {
					show: false
				},
				type: 'category',
				nameTextStyle: {
					color: '#A0A0A0'
				}
			},
			yAxis: [
				{
					max: function (value) {
						return waterMaxMin.max
					},
					min: function (value) {
						return waterMaxMin.min
					},
					axisLabel: {
						show: true,
						textStyle: {
							color: '#A0A0A0'
						}
					},
					axisLine: {
						show: true,
						lineStyle: {
							color: '#A0A0A0',
							width: 1
						}
					},
					name: '水位(m)',
					axisTick: {
						show: false
					},
					splitLine: {
						lineStyle: {
							color: '#A0A0A0',
							width: 1
						}
					},
					type: 'value',
					nameTextStyle: {
						color: '#A0A0A0'
					}
				}
			],
			legend: {
				data: legendName,
				type: 'scroll',
				bottom: '0',
				textStyle: {
					fontSize: 10
				}
			},
			tooltip: {
				trigger: 'axis',
				formatter: (params) => {
					var relVal = params[0].name
					for (var i = 0, l = params.length; i < l; i++) {
						let unit = 'm'
						if (['流量', '出库流量', '入库流量'].includes(params[i].seriesName)) {
							unit = 'm³/s'
						}
						relVal += '<br/>' + params[i].marker + params[i].seriesName + ':' + (params[i].value || '-') + unit
					}
					return relVal
				}
			},
			grid: {
				top: '15%',
				left: '10%',
				bottom: '24%',
				right: '10%'
			},
			series: series
		}

		// 有流量加上右侧指标
		let item = series.find((item) => ['流量', '出库流量', '入库流量'].includes(item.name))
		if (item) {
			let yAxisLiulang = {
				max: function (value) {
					// return liuliangMaxMin.max
					return parseInt(liuliangMaxMin.max * 1.1)
					// liuliangMaxMin.max = parseInt(max * 1.1)
					// liuliangMaxMin.min = parseInt(min * 0.9)
				},
				min: function (value) {
					return parseInt(liuliangMaxMin.min * 0.9)
				},
				position: 'right',
				axisLabel: {
					show: true,
					textStyle: {
						color: '#A0A0A0'
					}
				},
				axisLine: {
					show: true,
					lineStyle: {
						color: '#A0A0A0',
						width: 1
					}
				},
				name: '流量(m³/s)',
				axisTick: {
					show: false
				},
				splitLine: {
					lineStyle: {
						color: '#A0A0A0',
						width: 1
					}
				},
				type: 'value',
				nameTextStyle: {
					color: '#A0A0A0'
				}
			}
			option.yAxis.push(yAxisLiulang)
		}
		return { table, option }
	}
}
